//
//  ViewController.m
//  11206
//
//  Created by student on 16/3/3.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSDictionary *teams;
@property (nonatomic,strong) NSArray *orders;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic,strong) UISearchController *searchcontroller;
@property (nonatomic,strong) NSMutableArray *fliterNames;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"team dictionary" ofType:@"plist"];
    self.teams = [NSDictionary dictionaryWithContentsOfFile:path];
    self.orders = [[self.teams allKeys]sortedArrayUsingSelector:@selector(compare:)];
    
    self.searchcontroller = [[UISearchController alloc]initWithSearchResultsController:nil];
    self.searchcontroller.searchResultsUpdater = self;
    self.tableview.tableHeaderView = self.searchcontroller.searchBar;
        // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchcontroller.active)
    {
        return self.fliterNames.count;
    }
    else
    {
        NSString *group = self.orders[section];
        NSArray *selectedgroup = self.teams[group];
        return selectedgroup.count;
    }
    
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.searchcontroller.active)
    {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.text = self.fliterNames[indexPath.row];
        return cell;
    }
    else
    {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        NSString *group = self.orders[indexPath.section];
        NSArray *selectedgroup = self.teams[group];
        cell.textLabel.text = selectedgroup[indexPath.row];
        return cell;
    }
}

//@optional

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.searchcontroller.active)
    {
        return 1;
    }
    else
    {
        return self.teams.count;
    }
}// Default is 1 if not implemented

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *key = self.orders[section];
    return key;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
        return self.orders;
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchstr = searchController.searchBar.text;
    if (searchstr.length > 0)
    {
        self.fliterNames = [[NSMutableArray alloc]init];
//        NSPredicate *pre = [NSPredicate predicateWithFormat:@"self contains %@",searchstr];
        NSPredicate *pre = [NSPredicate predicateWithBlock:^BOOL(NSString *name, NSDictionary<NSString *,id> * _Nullable bindings) {
            NSRange range = [name rangeOfString:searchstr options:NSCaseInsensitiveSearch];
            return range.location != NSNotFound;
        }];
        NSArray *keys = [[self.teams allKeys]sortedArrayUsingSelector:@selector(compare:)];
        for (NSString *tmp in keys)
        {
            NSLog(@"%@",tmp);
            NSArray *names = self.teams[tmp];
            NSArray *tmp2 = [names filteredArrayUsingPredicate:pre];
            [self.fliterNames addObjectsFromArray:tmp2];
        }
        
       
    }
    [self.tableview reloadData];
}
@end
